package Demo;

public class Crossover 
{

	public static String[] OnePoint(String a, String b, int point)
	{
		String[] out = {"",""};
		
		if(point < a.length() && point < b.length())
		{
			out[0] = a.substring(0, point) + b.substring(point);
			out[1] = b.substring(0, point) + a.substring(point);
		}
		return out;
	}
	
	public static String[] TwoPoint(String a, String b, int pnt1, int pnt2)
	{
		String[] out = {"",""};
		if(pnt1 < a.length() && pnt1 < b.length())
		{
			out = OnePoint(a,b,pnt1);
		}
		if(pnt2 < a.length() && pnt2 < b.length())
		{
			out = OnePoint(out[0],out[1],pnt2);
		}
		return out;
	}
	
	public static String[] NPoint(String a, String b, int numPoints)
	{
		String[] out = {a,b};
		int minLength = Math.min(a.length(), b.length());
		
		for(int i=0;i<numPoints;i++)
		{
			int point = (int) (Math.random() * minLength) - 1;
			
			out = OnePoint(out[0],out[1],point);
			
		}
		return out;
	}
	
	public static String[] Uniform(String a, String b)
	{
		String[] out = {"",""};
		int minLength = Math.min(a.length(), b.length());
		double rate = .2;
		
		for (int i = 0; i < minLength; i++)
		{
			if(Math.random()<rate)
			{
				out[1]+=a.charAt(i);
				out[0]+=b.charAt(i);
				
			}
			else
			{
				out[0]+=a.charAt(i);
				out[1]+=b.charAt(i);
			}
		}
		return out;
	}
	
	public static String Mutate(String chromosome, double rate)
	{
		String out = "";
		
		for (int i = 0; i < chromosome.length(); i++)
		{
			if(Math.random()<rate)
			{
				if(chromosome.charAt(i)=='0')
				{
					out+='1';
				}
				else
				{
					out += '0';
				}
			}
			else
			{
				out += chromosome.charAt(i);
			}
		}
		
		return out;
	}
	
}
